package com.kuang.controller;

import com.kuang.mapper.UserMapper;
import com.kuang.service.UserServiceImpl;
import com.kuang.springcloud.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@RestController
//@Controller
public class UserController {
/**
 * @description：TODO

 * @author     ：LK
 * @date       ：Created in 2021-10-26 15:11
 */
@Autowired
UserMapper userMapper;
    @Autowired
    UserServiceImpl userServiceImpl;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("/test")
    public String TestMysql2Postgre1(){
        return "666";
    }


    @RequestMapping("/Mysql2Postgre/getmysql2redis")
        public String TestMysql2Postgre(int  id){
            /*
             *
             *  mysql准换到postgre中转换策略：
             *  把mysql中的数据取出来，放在redis中，设置key值，然后把redis中的数据放在postgre中
             *
             * */

            //把mysql中的数据取出来
            User userBase = userMapper.findUserById(id);
            System.out.println("从mysql数据库中取出的人是"+userBase.getUsername());
//            System.out.println("从mysql数据库中取出的人是"+userBase.getUsername());

            //把mysql取出的数据放在redis中
            ValueOperations<String,User> valueOperations = redisTemplate.opsForValue();
            // (key-value )== (userId-User)
            valueOperations.set(userBase.getUsername(),userBase,5, TimeUnit.HOURS);
            System.out.println("在redis中存入的数据用户是"+userBase.getUsername());

            return "lk牛逼！";
    }


    }
